home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 59 / 59.xpi / chrome / useragentswitcher.jar / content / useragentswitcher / useragentswitcher.js < prev    next >
Text File  |  2009-06-30  |  17KB  |  499 lines

  1. // User Agent Switcher
  2. var UserAgentSwitcher = 
  3. {
  4.     // Displays the about dialog
  5.     about: function()
  6.     {
  7.         window.openDialog("chrome://useragentswitcher/content/about/about.xul", "useragentswitcher-about-dialog", "centerscreen,chrome,modal");
  8.     },
  9.  
  10.     // Called when a button has been dropped
  11.     buttonDrop: function(event)
  12.     {
  13.         nsDragAndDrop.drop(event, UserAgentSwitcher);
  14.     },
  15.     
  16.     // Changes the options
  17.     changeOptions: function()
  18.     {
  19.         var hideMenuPreference = false;
  20.         var menu               = document.getElementById("useragentswitcher-menu");
  21.     
  22.         // If the hide menu preference is set
  23.         if(UserAgentSwitcherPreferences.isPreferenceSet("useragentswitcher.menu.hide"))
  24.         {
  25.             hideMenuPreference = UserAgentSwitcherPreferences.getBooleanPreference("useragentswitcher.menu.hide", true);
  26.         }
  27.     
  28.         // If the menu exists
  29.         if(menu)
  30.         {
  31.             menu.setAttribute("hidden", hideMenuPreference);
  32.         }
  33.     },
  34.     
  35.     // Finds the selected user agent and returns it's position and description
  36.     findSelectedUserAgent: function()
  37.     {
  38.         var selectedUserAgent = {};
  39.         var userAgent         = null;
  40.         var userAgents        = document.getElementById("useragentswitcher-popup-menu").getElementsByAttribute("type", "radio");
  41.         var userAgentsLength  = userAgents.length;
  42.     
  43.         selectedUserAgent.description = "";
  44.         selectedUserAgent.position    = "";
  45.     
  46.         // Loop through the user agents
  47.         for(var i = 0; i < userAgentsLength; i++)
  48.         {
  49.             userAgent = userAgents.item(i);
  50.             
  51.             // If this is the selected user agent
  52.             if(UserAgentSwitcher.isSelectedUserAgent(userAgent))
  53.             {
  54.                 selectedUserAgent.description = userAgent.getAttribute("label");
  55.                 selectedUserAgent.position    = i;
  56.             
  57.                 break;
  58.             }
  59.         }
  60.     
  61.         return selectedUserAgent;
  62.     },
  63.     
  64.     // Returns the selected user agents for a particular menu
  65.     getIndividualSelectedUserAgents: function(menu)
  66.     {
  67.         var userAgents = [];
  68.  
  69.         // If the menu is set
  70.         if(menu)
  71.         {
  72.             var subMenus       = menu.getElementsByTagName("menu");
  73.             var subMenusLength = subMenus.length;
  74.         
  75.             userAgents = userAgents.concat(UserAgentSwitcherArray.convertCollectionToArray(menu.getElementsByAttribute("checked", "true")));
  76.  
  77.             // Loop through the sub menus
  78.             for(var i = 0; i < subMenusLength; i++)
  79.             {
  80.                 userAgents = userAgents.concat(UserAgentSwitcher.getIndividualSelectedUserAgents(subMenus[i].firstChild));
  81.             }
  82.         }
  83.  
  84.         return userAgents;
  85.     },
  86.     
  87.     // Returns the selected user agents
  88.     getSelectedUserAgents: function(windowDocument)
  89.     {
  90.         return UserAgentSwitcher.getIndividualSelectedUserAgents(windowDocument.getElementById("useragentswitcher-popup-menu")).concat(UserAgentSwitcher.getIndividualSelectedUserAgents(windowDocument.getElementById("useragentswitcher-popup-toolbar")));
  91.     },
  92.     
  93.     // Return the supported flavours
  94.     getSupportedFlavours: function() 
  95.     {
  96.       var flavourSet = new FlavourSet();
  97.  
  98.     flavourSet.appendFlavour("text/toolbarwrapper-id/" + document.documentElement.id);
  99.  
  100.       return flavourSet;
  101.      },
  102.    
  103.     // Opens the help
  104.     help: function()
  105.     {
  106.         window.getBrowser().selectedTab = window.getBrowser().addTab("http://chrispederick.com/work/user-agent-switcher/help/");
  107.     },
  108.     
  109.     // Initializes the extension
  110.     initialize: function(event)
  111.     {
  112.         // Try to get the window content
  113.         try
  114.         {
  115.             var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  116.  
  117.             UserAgentSwitcherUpgrade.upgrade();
  118.             UserAgentSwitcherImporter.import(UserAgentSwitcherImporter.importTypeMenu, UserAgentSwitcherImporter.getUserAgentFileLocation(), true);
  119.             UserAgentSwitcher.initializeDisplay();
  120.     
  121.             // If the observer service is set
  122.             if(observerService)
  123.             {
  124.                 observerService.addObserver(UserAgentSwitcher, "quit-application-requested", false);
  125.             }
  126.     
  127.             document.getElementById("navigator-toolbox").addEventListener("dragdrop", UserAgentSwitcher.buttonDrop, false);
  128.             window.removeEventListener("load", UserAgentSwitcher.initialize, false);
  129.         }
  130.         catch(exception)
  131.         {
  132.             UserAgentSwitcherLog.log("UserAgentSwitcher.initialize", exception);
  133.         }
  134.     },
  135.  
  136.     // Initializes the display
  137.     initializeDisplay: function()
  138.     {
  139.         var allWindows           = UserAgentSwitcherDOM.getAllWindows();
  140.         var allWindowsLength     = allWindows.length;
  141.         var defaultUserAgent     = null;
  142.         var position             = null;
  143.         var userAgentDescription = null;
  144.         var userAgentSet         = false;
  145.  
  146.         // If the user agent override preference is set
  147.         if(UserAgentSwitcherPreferences.isPreferenceSet("general.useragent.override"))
  148.         {
  149.             var selectedUserAgent = UserAgentSwitcher.findSelectedUserAgent();
  150.  
  151.             position             = selectedUserAgent.position;
  152.             userAgentDescription = selectedUserAgent.description;
  153.             userAgentSet         = true;
  154.         }
  155.         else
  156.         {
  157.             defaultUserAgent = UserAgentSwitcherStringBundle.getString("defaultUserAgent");
  158.         }
  159.  
  160.         // Loop through the open windows
  161.         for(var i = 0; i < allWindowsLength; i++)
  162.         {
  163.             // If a user agent is set
  164.             if(userAgentSet)
  165.             {
  166.                 UserAgentSwitcher.selectUserAgent(allWindows[i].document, position, userAgentDescription);
  167.             }
  168.             else
  169.             {
  170.                 UserAgentSwitcher.resetUserAgent(allWindows[i].document, defaultUserAgent);
  171.             }
  172.         }        
  173.     },
  174.  
  175.     // Returns true if this is the selected user agent
  176.     isSelectedUserAgent: function(userAgent)
  177.     {
  178.         // If all the attributes match
  179.         if((UserAgentSwitcherPreferences.getStringPreference("general.useragent.appName", true) == userAgent.getAttribute("useragentswitcherappcodename") || (UserAgentSwitcherPreferences.getStringPreference("general.useragent.appName", true) == " " && userAgent.getAttribute("useragentswitcherappcodename") == "")) &&    
  180.               UserAgentSwitcherPreferences.getStringPreference("general.appname.override", true) == userAgent.getAttribute("useragentswitcherappname") &&
  181.               UserAgentSwitcherPreferences.getStringPreference("general.appversion.override", true) == userAgent.getAttribute("useragentswitcherappversion") &&
  182.                UserAgentSwitcherPreferences.getStringPreference("general.platform.override", true) == userAgent.getAttribute("useragentswitcherplatform") &&
  183.               UserAgentSwitcherPreferences.getStringPreference("general.useragent.override", true) == userAgent.getAttribute("useragentswitcheruseragent") &&
  184.               UserAgentSwitcherPreferences.getStringPreference("general.useragent.vendor", true) == userAgent.getAttribute("useragentswitchervendor") &&
  185.               UserAgentSwitcherPreferences.getStringPreference("general.useragent.vendorSub", true) == userAgent.getAttribute("useragentswitchervendorsub"))
  186.         {
  187.             return true;
  188.         }
  189.     
  190.         return false;
  191.     },
  192.  
  193.     // Observes quits
  194.     observe: function(subject, topic, data)
  195.     {
  196.         // If the reset on close preference is not set or is set to true
  197.         if(!UserAgentSwitcherPreferences.isPreferenceSet("useragentswitcher.reset.onclose") || UserAgentSwitcherPreferences.getBooleanPreference("useragentswitcher.reset.onclose", true))
  198.         {
  199.             UserAgentSwitcher.reset();
  200.         }
  201.  
  202.         return false;
  203.     },
  204.     
  205.     // Called when a button has been dropped
  206.     onDrop: function (event, transferData, session) 
  207.     {
  208.         // If the User Agent Switcher button was dropped
  209.       if(transferData.data == "useragentswitcher-button")
  210.       {
  211.             UserAgentSwitcherImporter.import(UserAgentSwitcherImporter.importTypeMenu, UserAgentSwitcherImporter.getUserAgentFileLocation(), true);
  212.             UserAgentSwitcher.initializeDisplay();            
  213.       }
  214.     },
  215.     
  216.     // Opens a toolbar button automatically if another toolbar button is open on the toolbar
  217.     openToolbarButton: function(currentToolbarButton)
  218.     {
  219.         // If the toolbar button is set and is not open
  220.         if(currentToolbarButton && !currentToolbarButton.open)
  221.         {
  222.             var toolbarButton        = null;
  223.             var toolbarButtons       = currentToolbarButton.parentNode.getElementsByTagName("toolbarbutton");
  224.             var toolbarButtonsLength = toolbarButtons.length;
  225.     
  226.             // Loop through the toolbar buttons
  227.             for(var i = 0; i < toolbarButtonsLength; i++)
  228.             {
  229.                 toolbarButton = toolbarButtons.item(i);
  230.     
  231.                 // If the toolbar button is set, is not the same toolbar button and is open
  232.                 if(toolbarButton && toolbarButton != currentToolbarButton && toolbarButton.open)
  233.                 {
  234.                     toolbarButton.open        = false;
  235.                     currentToolbarButton.open = true;
  236.     
  237.                     break;
  238.                 }
  239.             }
  240.         }
  241.     },
  242.     
  243.     // Displays the options dialog
  244.     options: function()
  245.     {
  246.         window.openDialog("chrome://useragentswitcher/content/options/options.xul", "useragentswitcher-options-dialog", "centerscreen,chrome,modal,resizable");
  247.     
  248.         UserAgentSwitcher.changeOptions();
  249.     },
  250.     
  251.     // Resets the user agent
  252.     reset: function()
  253.     {
  254.         var allWindows       = UserAgentSwitcherDOM.getAllWindows();
  255.         var allWindowsLength = allWindows.length;
  256.         var defaultUserAgent = UserAgentSwitcherStringBundle.getString("defaultUserAgent");
  257.  
  258.         // If an override app code name is set
  259.         if(UserAgentSwitcherPreferences.isPreferenceSet("general.useragent.appName"))
  260.         {
  261.             UserAgentSwitcherPreferences.deletePreference("general.useragent.appName");
  262.         }
  263.         
  264.         // If an override app name is set
  265.         if(UserAgentSwitcherPreferences.isPreferenceSet("general.appname.override"))
  266.         {
  267.             UserAgentSwitcherPreferences.deletePreference("general.appname.override");
  268.         }
  269.     
  270.         // If an override app version is set
  271.         if(UserAgentSwitcherPreferences.isPreferenceSet("general.appversion.override"))
  272.         {
  273.             UserAgentSwitcherPreferences.deletePreference("general.appversion.override");
  274.         }
  275.     
  276.         // If an override platform is set
  277.         if(UserAgentSwitcherPreferences.isPreferenceSet("general.platform.override"))
  278.         {
  279.             UserAgentSwitcherPreferences.deletePreference("general.platform.override");
  280.         }
  281.     
  282.         // If an override user agent is set
  283.         if(UserAgentSwitcherPreferences.isPreferenceSet("general.useragent.override"))
  284.         {
  285.             UserAgentSwitcherPreferences.deletePreference("general.useragent.override");
  286.         }
  287.     
  288.         // If an override vendor is set
  289.         if(UserAgentSwitcherPreferences.isPreferenceSet("general.useragent.vendor"))
  290.         {
  291.             UserAgentSwitcherPreferences.deletePreference("general.useragent.vendor");
  292.         }
  293.     
  294.         // If an override vendor sub is set
  295.         if(UserAgentSwitcherPreferences.isPreferenceSet("general.useragent.vendorSub"))
  296.         {
  297.             UserAgentSwitcherPreferences.deletePreference("general.useragent.vendorSub");
  298.         }
  299.  
  300.         // Loop through the open windows
  301.         for(var i = 0; i < allWindowsLength; i++)
  302.         {
  303.             UserAgentSwitcher.resetUserAgent(allWindows[i].document, defaultUserAgent);
  304.         }
  305.     },
  306.  
  307.     // Resets the user agent for a window
  308.     resetUserAgent: function(windowDocument, defaultUserAgent)
  309.     {
  310.         var defaultMenu              = windowDocument.getElementById("useragentswitcher-default-menu");
  311.         var defaultToolbar           = windowDocument.getElementById("useragentswitcher-default-toolbar");
  312.         var selectedUserAgent        = null;
  313.         var selectedUserAgents       = UserAgentSwitcher.getSelectedUserAgents(windowDocument);
  314.         var selectedUserAgentsLength = selectedUserAgents.length;
  315.         var userAgentButton          = windowDocument.getElementById("useragentswitcher-button");
  316.         var userAgentMenu            = windowDocument.getElementById("useragentswitcher-menu");
  317.  
  318.         // If the user agent menu exists
  319.         if(userAgentMenu)
  320.         {
  321.             userAgentMenu.setAttribute("image", "chrome://useragentswitcher/skin/default.png");
  322.             userAgentMenu.setAttribute("label", defaultUserAgent);
  323.         }
  324.  
  325.         // If the user agent button is set
  326.         if(userAgentButton)
  327.         {    
  328.             // If the user agent button has a default attribute
  329.             if(userAgentButton.hasAttribute("default"))
  330.             {
  331.                 userAgentButton.removeAttribute("default");
  332.             }        
  333.  
  334.             userAgentButton.setAttribute("label", defaultUserAgent);
  335.             userAgentButton.setAttribute("tooltiptext", defaultUserAgent);
  336.         }
  337.         
  338.         // Loop through the selected user agents
  339.         for(var i = 0; i < selectedUserAgentsLength; i++)
  340.         {
  341.             selectedUserAgent = selectedUserAgents[i];
  342.             
  343.             // If the selected user agent does not have an id or the id is not the default
  344.             if(!selectedUserAgent.hasAttribute("id") || selectedUserAgent.getAttribute("id").indexOf("useragentswitcher-default-") != 0)
  345.             {
  346.                 selectedUserAgent.removeAttribute("checked");
  347.             }
  348.         }
  349.  
  350.         // If the default menu exists
  351.         if(defaultMenu)
  352.         {
  353.             defaultMenu.setAttribute("checked", true);
  354.         }
  355.  
  356.         // If the default toolbar exists
  357.         if(defaultToolbar)
  358.         {
  359.             defaultToolbar.setAttribute("checked", true);
  360.         }
  361.     },
  362.  
  363.     // Selects the user agent for a window
  364.     selectUserAgent: function(windowDocument, position, selectedUserAgentDescription)
  365.     {
  366.         var positionMenu             = windowDocument.getElementById("useragentswitcher-user-agent-" + position + "-menu");
  367.         var positionToolbar          = windowDocument.getElementById("useragentswitcher-user-agent-" + position + "-toolbar");
  368.         var selectedUserAgent        = null;
  369.         var selectedUserAgents       = UserAgentSwitcher.getSelectedUserAgents(windowDocument);
  370.         var selectedUserAgentsLength = selectedUserAgents.length;
  371.         var userAgentButton          = windowDocument.getElementById("useragentswitcher-button");
  372.         var userAgentMenu            = windowDocument.getElementById("useragentswitcher-menu");
  373.  
  374.         // If the user agent menu exists
  375.         if(userAgentMenu)
  376.         {
  377.             userAgentMenu.setAttribute("image", "chrome://useragentswitcher/skin/non-default.png");
  378.  
  379.             // If the selected user agent description is set            
  380.             if(selectedUserAgentDescription)
  381.             {
  382.                 userAgentMenu.setAttribute("label", selectedUserAgentDescription);
  383.             }
  384.         }
  385.  
  386.         // If the user agent button is set
  387.         if(userAgentButton)
  388.         {    
  389.             userAgentButton.setAttribute("default", "false");
  390.  
  391.             // If the selected user agent description is set            
  392.             if(selectedUserAgentDescription)
  393.             {
  394.                 userAgentButton.setAttribute("label", selectedUserAgentDescription);
  395.                 userAgentButton.setAttribute("tooltiptext", selectedUserAgentDescription);
  396.             }
  397.         }
  398.         
  399.         // Loop through the selected user agents
  400.         for(var i = 0; i < selectedUserAgentsLength; i++)
  401.         {
  402.             selectedUserAgent = selectedUserAgents[i];
  403.             
  404.             // If the selected user agent does not have an id or the id does not match the position
  405.             if(!selectedUserAgent.hasAttribute("id") || selectedUserAgent.getAttribute("id").indexOf("useragentswitcher-user-agent-" + position + "-") != 0)
  406.             {
  407.                 selectedUserAgent.removeAttribute("checked");
  408.             }
  409.         }
  410.  
  411.         // If the position menu exists
  412.         if(positionMenu)
  413.         {
  414.             positionMenu.setAttribute("checked", true);
  415.         }
  416.  
  417.         // If the position toolbar exists
  418.         if(positionToolbar)
  419.         {
  420.             positionToolbar.setAttribute("checked", true);
  421.         }
  422.     },
  423.     
  424.     // Switches the user agent
  425.     switchUserAgent: function(userAgent)
  426.     {
  427.         var allWindows           = UserAgentSwitcherDOM.getAllWindows();
  428.         var allWindowsLength     = allWindows.length;
  429.         var appCodeName          = userAgent.getAttribute("useragentswitcherappcodename");
  430.         var position             = userAgent.getAttribute("useragentswitcherposition");
  431.         var userAgentDescription = userAgent.getAttribute("label");
  432.         
  433.         // If the app code name is not set add a space since an empty app code name is ignored
  434.         if(!appCodeName)
  435.         {
  436.             appCodeName = " ";
  437.         }
  438.         
  439.         UserAgentSwitcherPreferences.setStringPreference("general.useragent.appName", appCodeName);
  440.         UserAgentSwitcherPreferences.setStringPreference("general.appname.override", userAgent.getAttribute("useragentswitcherappname"));
  441.         UserAgentSwitcherPreferences.setStringPreference("general.appversion.override", userAgent.getAttribute("useragentswitcherappversion"));
  442.         UserAgentSwitcherPreferences.setStringPreference("general.platform.override", userAgent.getAttribute("useragentswitcherplatform"));
  443.         UserAgentSwitcherPreferences.setStringPreference("general.useragent.override", userAgent.getAttribute("useragentswitcheruseragent"));
  444.         UserAgentSwitcherPreferences.setStringPreference("general.useragent.vendor", userAgent.getAttribute("useragentswitchervendor"));
  445.         UserAgentSwitcherPreferences.setStringPreference("general.useragent.vendorSub", userAgent.getAttribute("useragentswitchervendorsub"));
  446.  
  447.         // Loop through the open windows
  448.         for(var i = 0; i < allWindowsLength; i++)
  449.         {
  450.             UserAgentSwitcher.selectUserAgent(allWindows[i].document, position, userAgentDescription);
  451.         }
  452.     },
  453.     
  454.     // Opens the test page
  455.     test: function()
  456.     {
  457.         window.getBrowser().selectedTab = window.getBrowser().addTab("http://chrispederick.com/work/user-agent-switcher/help/test/");
  458.     },
  459.     
  460.     // Uninitializes the extension
  461.     uninitialize: function(event)
  462.     {
  463.         // Try to get the window content
  464.         try
  465.         {
  466.             var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  467.     
  468.             // If the observer service is set
  469.             if(observerService)
  470.             {
  471.                 observerService.removeObserver(UserAgentSwitcher, "quit-application-requested", false);
  472.             }
  473.     
  474.             // If the reset on close preference is not set or is set to true
  475.             if(!UserAgentSwitcherPreferences.isPreferenceSet("useragentswitcher.reset.onclose") || UserAgentSwitcherPreferences.getBooleanPreference("useragentswitcher.reset.onclose", true))
  476.             {
  477.                 var allWindows  = UserAgentSwitcherDOM.getAllWindows();
  478.                 var windowCount = allWindows.length;
  479.     
  480.                 // If this is the last window closing
  481.                 if(windowCount == 0)
  482.                 {
  483.                     UserAgentSwitcher.reset();
  484.                 }
  485.             }
  486.     
  487.             document.getElementById("navigator-toolbox").removeEventListener("dragdrop", UserAgentSwitcher.buttonDrop, false);    
  488.             window.removeEventListener("close", UserAgentSwitcher.uninitialize, false);
  489.         }
  490.         catch(exception)
  491.         {
  492.             UserAgentSwitcherLog.log("UserAgentSwitcher.uninitialize", exception);
  493.         }
  494.     }
  495. };
  496.  
  497. window.addEventListener("load", UserAgentSwitcher.initialize, false);
  498. window.addEventListener("unload", UserAgentSwitcher.uninitialize, false);
  499.